//
//  DetailDataBase.m
//  NewProject
//
//  Created by dlios on 15/3/9.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "DetailDataBase.h"

@implementation DetailDataBase

+(instancetype)shareDetailDataBaseHandle
{
    static DetailDataBase *detailDBH = nil;
    
    if (detailDBH == nil) {
        detailDBH = [[DetailDataBase alloc] init];
    }
    return detailDBH;
}

static sqlite3 *detailDB;

-(void)openDB
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFielPath = [array lastObject];
    
    NSString *sqlitePath = [documentFielPath stringByAppendingPathComponent:@"detail.sqlite"];
    
    NSLog(@"99999999 %@", sqlitePath);
    if (detailDB != nil) {
        return;
    }
    
    int result = sqlite3_open(sqlitePath.UTF8String, &detailDB);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }

}

-(void)createTable
{
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS detailClass(number INTEGER PRIMARY KEY AUTOINCREMENT, ingredient TEXT,  ing_clear TEXT, direction TEXT, duration TEXT, favourite TEXT)";
    
    // 执行sql语句
    int result = sqlite3_exec(detailDB, createSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }

}

-(void)insertDetail:(DetailModel *)detail
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO detailClass(ingredient, ing_clear, direction, duration, favourite)  VALUES('%@', '%@', '%@', '%@', '%@')", detail.ingredient, detail.ing_clear, detail.direction, detail.duration, detail.favourite];
    
    //  执行sql语句
    int result = sqlite3_exec(detailDB, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }

}

-(void)deleteDetail:(NSInteger)number
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM detailClass WHERE number = '%ld'", (long)number];
    
    int result = sqlite3_exec(detailDB, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }

    
}

-(NSMutableArray *)selectDetail
{
    NSString *selectSql = @"SELECT * FROM detailClass";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(detailDB, selectSql.UTF8String, -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            int number = sqlite3_column_int(stmt, 0);
            const unsigned char *ingredient = sqlite3_column_text(stmt, 1);
            const unsigned char *ing_clear = sqlite3_column_text(stmt, 2);
            const unsigned char *direction = sqlite3_column_text(stmt, 3);
            const unsigned char *duration = sqlite3_column_text(stmt, 4);
            const unsigned char *favourite = sqlite3_column_text(stmt, 5);

            NSInteger numberInteger = number;
            NSString *ingredientStr = [NSString stringWithUTF8String:(const char *)ingredient];
            NSString *ing_clearStr = [NSString stringWithUTF8String:(const char *)ing_clear];
            NSString *directionStr = [NSString stringWithUTF8String:(const char *)direction];
            NSString *favouriteStr = [NSString stringWithUTF8String:(const char *)favourite];
            NSString *durationStr = [NSString stringWithUTF8String:(const char *)duration];

//            
//            MaterialModel *material = [[MaterialModel alloc] init];
//            
//            material.name = nameStr;
//            material.url = urlStr;
//            material.thumb = thumbStr;
//            material.number = numberInteger;
//            
//            [array addObject:material];
            
            DetailModel *detail = [[DetailModel alloc] init];
            
            detail.ingredient = ingredientStr;
            detail.ing_clear = ing_clearStr;
            detail.direction = directionStr;
            detail.duration = durationStr;
            detail.favourite = favouriteStr;
            detail.number = numberInteger;
            [array addObject:detail];
        }
        
    }else
    {
        NSLog(@"查询准备失败");
    }
    
    return array;

}

-(void)deleteAllObject
{
    NSString *deleteBase = @"DELETE * FROM detailClass";
    
    
    int result = sqlite3_exec(detailDB, deleteBase.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"delete success");
    }else{
        NSLog(@"fail");
    }
    

}

-(void)deleteTable
{
    NSString *deleteTabel = @"DROP TABLE detailClass";
    
    int result = sqlite3_exec(detailDB, deleteTabel.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }else
    {
        NSLog(@"失败");
    }

}


@end
